package com.zjq.service;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zjq.hibernate.pojo.Log;
import com.zjq.hibernate.pojo.User;
import com.zjq.hibernatefactory.HibernateSessionFactory;

@Path("bs")
public class BankService {
	@GET
	@Path("login/{Username},{Userpwd}")
	@Produces(MediaType.APPLICATION_JSON)
	public String loginService(@PathParam("Username")String uname,@PathParam("Userpwd")String upwd){
		Session session = HibernateSessionFactory.getSession();
		String hsql = "from User u where u.username = ?";
		Query query = session.createQuery(hsql);
		query.setString(0, uname);
		User user = (User)query.uniqueResult();
		HibernateSessionFactory.closeSession();
		if((user==null)||(!user.getUserpwd().equals(upwd))){
			return "{\"login\":[{\"state\":\"0\"}]}";
		}
		return "{\"login\":[{\"state\":\"1\"}]}";
	}
	
	
	@GET
	@Path("zhifu/{cardid},{cardpwd},{money}")
	@Produces(MediaType.APPLICATION_JSON)
	public String zhifuService(@PathParam("cardid") String cardid,@PathParam("cardpwd") String cardpwd,@PathParam("money") String money){
		int type = -1;
		try {
			Session session = HibernateSessionFactory.getSession();
			Transaction t = session.beginTransaction();
			
			String hsql = "from User u where u.level = ?";
			Query query = session.createQuery(hsql);
			query.setInteger(0, 0);
			User out = (User)query.uniqueResult();
			String hsql2 = "from User u where u.bankcard.cardid = ?";
			Query query2 = session.createQuery(hsql2);
			query2.setInteger(0, Integer.parseInt(cardid));
			User in = (User)query2.uniqueResult();
			if(in == null){
				type = 1;//用户输入的卡号不对
			}else if (!in.getBankcard().getCardpwd().equals(cardpwd)) {
				type = 2;//用户输入的卡号和密码不匹配
			}else if (in.getBankcard().getMoney()<Integer.parseInt(money)) {
				type = 3;//用户卡里的钱不足以支付
			}else {
				float outmoney =Float.parseFloat(money)+out.getBankcard().getMoney();
				float inmoney =in.getBankcard().getMoney()-Float.parseFloat(money);
				out.getBankcard().setMoney(outmoney);//修改卖家卡里的钱   
				in.getBankcard().setMoney(inmoney);//修改买家卡里的钱
				session.update(out);
				session.update(in);
				type = 0;
			}
			Log log = new Log(out,in,Float.parseFloat(money), type);
			session.save(log);
			t.commit();
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HibernateSessionFactory.closeSession();
		String msg = "";
		switch (type) {
		case 0:
			msg = "支付成功";
			break;
		case 1:
			msg = "输入的卡号不对";
			break;
		case 2:
		    msg = "输入的卡号和密码不匹配";
			break;
		case 3:
			msg = "卡里的钱不足以支付";
			break;
		default:
			msg = "系统出现异常，请联系工作人员";
			break;
		}
		return "{\"zhifu\":[{\"msg\":\""+msg+"\"}]}";
	}
	
	
	
	
	@GET
	@Path("tuikuan/{cardid},{money}")
	@Produces(MediaType.APPLICATION_JSON)
	public String tuikuanService(@PathParam("cardid") String cardid,@PathParam("money") String money){
		Session session = HibernateSessionFactory.getSession();
		String hsql = "from User u where u.level = ?";
		Query query = session.createQuery(hsql);
		query.setInteger(0, 0);
		User out = (User)query.uniqueResult();
		String hsql2 = "from User u where u.bankcard.cardid = ?";
		Query query2 = session.createQuery(hsql2);
		query2.setInteger(0, Integer.parseInt(cardid));
		User in = (User)query2.uniqueResult();
		int type = -1;
		if(in == null){
			type = 1;//用户输入的卡号不对
		}else if (out.getBankcard().getMoney()<Integer.parseInt(money)) {
			type = 3;//卖家卡里的钱不足退款
		}else {
			Transaction t = session.beginTransaction();
			float inmoney =Float.parseFloat(money)+in.getBankcard().getMoney();
			float outmoney =out.getBankcard().getMoney()-Float.parseFloat(money);
			out.getBankcard().setMoney(outmoney);//修改卖家卡里的钱   
			in.getBankcard().setMoney(inmoney);//修改买家卡里的钱
			session.update(out);
			session.update(in);
			t.commit();
			type = 0;
		}
		Log log = new Log(in, out, Float.parseFloat(money), type);
		session.save(log);
		HibernateSessionFactory.closeSession();
		String msg = "";
		switch (type) {
		case 0:
			msg = "退款成功";
			break;
		case 1:
			msg = "输入的卡号不对";
			break;
		case 3:
			msg = "卡里的钱不足以退款";
			break;
		default:
			msg = "系统出现异常，请联系工作人员";
			break;
		}
		return "{\"tuikuan\":[{\"msg\":\""+msg+"\"}]}";
	}
	
	
	@GET
	@Path("qingzhang")
	@Produces(MediaType.APPLICATION_JSON)
	public String qingzhangService(){
		Session session = HibernateSessionFactory.getSession();
		String hsql = "from Log l where l.type = ?";
		Query query = session.createQuery(hsql);
		query.setInteger(0, 0);
		List<Log> arr = new ArrayList<Log>();
		arr = query.list();
		float tuikuan=0f,shouru=0f;
		for (Log log : arr) {
			if(log.getUserByShoukuanid().getLevel()==0){
				shouru = shouru + log.getMoney();
			}else {
				tuikuan = tuikuan + log.getMoney();
			}
		}
		float msg = shouru - tuikuan;
		return "{\"qingzhang\":[{\"lirun\":\""+msg+"\"}]}";
	}
	
}
